<?php
abstract class SqliteAdapter extends SqlAdapter
{
    public function Select($table_list, $fields = null, $condition = null, $join = null, $orderby = null, $limit = null, &$page = 1, &$count = null, $groupby = null)
    {
        Log::LogQueryType('SELECT', $table_list);

        $this->AddQuotes($table_list);
        $fields = $this->GetSelectFields($fields);
        $where = $this->GetWhere($condition);
        $groupby = $this->GetGroupBy($groupby);
        $orderby = $this->GetOrderBy($orderby);

        $from = "$table_list
        $join
        $where";

        $select = "SELECT $fields
        FROM $from
        $groupby";

        $count = $this->GetCount($count, $groupby, $select, $from);
        $limit = $this->GetSelectLimit($limit, $page, $count);

        $sql = "$select
            $orderby
            $limit";
        return $this->Query($sql);
    }

    public function Describe($table)
    {
        return $this->Query("PRAGMA table_info('$table')");
    }

    public function GetFields($table)
    {
        $out = array();
        $fields = $this->Describe($table);
        foreach ($fields as $field)
        {
            $f['name']      = $field['name'];
            $f['type']      = $field['type'];
            $f['nullable']  = !$field['notnull'];
            $f['is_pk']     = $field['pk']!=0;
            $f['default']   = $field['dflt_value'];
            $f['auto_increment'] = false;
            $out[] = $f;
        }
        return $out;
    }

    public function ShowTables()
    {
        $tables = $this->Select('SQLITE_MASTER', 'name', "type='table'", null, 'name');
        return Set::Pluck($tables, 'name');
    }

    public function Truncate($table)
    {
        Log::LogQueryType('DELETE', $table);
        $this->AddQuotes($table);
        return $this->Execute("DELETE FROM $table");
    }
}
?>
